//
//  ChooseSeatsView.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-1.
//
//

#import "ChooseSeatsView.h"
#import "DeviceUtil.h"
#define SEATTAG 30000

@implementation ChooseSeatsView
{
    UIAlertView *_baseAlert;
    int choose_size;
}

- (void)dealloc
{
    [_baseAlert release];
    _baseAlert = nil;
    [super dealloc];
}


//static int choose_size = 0;

@synthesize seatlist;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        choose_size = 0;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

//加载视图
- (void)init_view
{
    
    NSInteger width = [DeviceUtil getScreenWidth];
    int rownum = [[seatlist objectAtIndex:0]count] + 1;
    int imglen = width / rownum * 0.9;
    int i;
    int j;
    for (i = 0; i < [seatlist count]; i++) {
        //说明排位号
        UILabel *colLbl = [[[UILabel alloc]initWithFrame:CGRectMake(0, i*width / rownum, imglen, imglen)]autorelease];
        if (i > 10) {
            colLbl.text = [NSString stringWithFormat:@"%d", i + 1];
        } else {
            colLbl.text = [NSString stringWithFormat:@"0%d", i + 1];
        }
        colLbl.backgroundColor = [UIColor clearColor];
        colLbl.textColor = [UIColor blueColor];
        colLbl.adjustsFontSizeToFitWidth = YES;
        [self addSubview:colLbl];
        
        //加载座位
        for (j = 0; j < rownum - 1; j++) {
            if ([[[seatlist objectAtIndex:i]objectAtIndex:j] isEqual:@"W"]) {
                UIImageView *imgview  = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sold"]]autorelease];
                imgview.frame = CGRectMake((j+1)*width / rownum, i*width / rownum, imglen, imglen);
                [self addSubview:imgview];
            } else if ([[[seatlist objectAtIndex:i]objectAtIndex:j] isEqual:@"N"]) {
                UIButton *imgbtn =[[[UIButton alloc]init]autorelease];
                imgbtn.frame = CGRectMake((j+1)*width / rownum, i*width / rownum, imglen, imglen);
                [imgbtn setBackgroundImage:[UIImage imageNamed:@"can_select"] forState:UIControlStateNormal];
                [imgbtn setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
                imgbtn.tag = SEATTAG + i*100 + j;
                [self addSubview:imgbtn];
                [imgbtn addTarget:self action:@selector(clickseat:) forControlEvents:UIControlEventTouchDown];
            } else if ([[[seatlist objectAtIndex:i]objectAtIndex:j] isEqual:@"S"]){
                UIButton *imgbtn =[[[UIButton alloc]init]autorelease];
                imgbtn.frame = CGRectMake((j+1)*width / rownum, i*width / rownum, imglen, imglen);
                [imgbtn setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
                [imgbtn setBackgroundImage:[UIImage imageNamed:@"can_select"] forState:UIControlStateSelected];
                imgbtn.tag = SEATTAG + i*100 + j;
                [self addSubview:imgbtn];
                [imgbtn addTarget:self action:@selector(cancelseat:) forControlEvents:UIControlEventTouchDown];
            }
        }
    }
}

//选择座位
- (void)clickseat:(id)sender
{
    //最多只能选择6个座位
    if (choose_size >= 5) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"太多了" message:@"一次最多只能选5张票哦亲"  delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
        return;
    }
    choose_size += 1;
    int tag = ((UIButton*)sender).tag;
    int j = tag % 100;
    int i = ((tag % SEATTAG) - j)/100;
    
    [((UIButton*)sender) setSelected:YES];
    [[seatlist objectAtIndex:i]replaceObjectAtIndex:j withObject:@"S"];
    
    [self.choosedelegate addTicketDelegate:[NSString stringWithFormat:@"%d", i] index2:[NSString stringWithFormat:@"%d", j] arraylist:seatlist];
}

//取消选择座位
- (void)cancelseat:(id)sender
{
    choose_size -= 1;
    int tag = ((UIButton*)sender).tag;
    int j = tag % 100;
    int i = ((tag % SEATTAG) - j)/100;
    [((UIButton*)sender) setSelected:YES];
    [[seatlist objectAtIndex:i]replaceObjectAtIndex:j withObject:@"N"];
    [self.choosedelegate delTicketDelegate:[NSString stringWithFormat:@"%d", i] index2:[NSString stringWithFormat:@"%d", j] arraylist:seatlist];
}

- (void) performDismiss: (NSTimer *)timer {
    [_baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [_baseAlert release];
    _baseAlert = NULL;
}

@end


